﻿using System;
using Iot.AlarmDispose;
using Iot.AlarmDispose.Database;
using Iot.RealTimeData;
using Iot.RealTimeData.RealTimeData;
using Iot.RealTimeData.RealTimeDataEvent;
using Iot.Shared.EventBusSend;
using Microsoft.Extensions.DependencyInjection;
using Prism.Ioc;

namespace Iot.Injection;

public static class Injection
{
    /// <summary>
    ///     元享模式  注入
    /// </summary>
    /// <param name="services"></param>
    /// <param name="info"></param>
    public static void RealTimeData<TIOnValueFactory>(this IServiceCollection services, RealTimeDataInfo info)
        where TIOnValueFactory : class, IOnValueFactory
    {
        services.AddSingleton<IEventBusSend, EventBusSendMediator>();
        services.AddSingleton<IOnValueFactory, TIOnValueFactory>();
        services.AddSingleton(typeof(IRealTimeData<,>), typeof(RealTimeData<,>));
        services.AddSingleton(info);
        services.AddSingleton<RealTimeDataAll>();
    }

    /// <summary>
    ///     元享模式  注入
    /// </summary>
    /// <param name="containerRegistry"></param>
    /// <param name="info"></param>
    public static void RealTimeData<TIOnValueFactory>(this IContainerRegistry containerRegistry, RealTimeDataInfo info)
        where TIOnValueFactory : class, IOnValueFactory
    {
        containerRegistry.RegisterSingleton<IEventBusSend, EventBusSendMediator>();
        containerRegistry.RegisterSingleton<IOnValueFactory, TIOnValueFactory>();
        containerRegistry.RegisterSingleton(typeof(IRealTimeData<,>), typeof(RealTimeData<,>));
        containerRegistry.RegisterSingleton<RealTimeDataInfo>(() => info);
        containerRegistry.RegisterSingleton<RealTimeDataAll>();
    }


    /// <summary>
    /// </summary>
    /// <param name="containerRegistry">IOC</param>
    /// <param name="alertsFactory">报警状态和分类创建告警数据模型接口</param>
    /// <param name="realTimeAlert">实时显示报警列表接口</param>
    /// <param name="alertClassifyDb">告警分类管理接口</param>
    /// <param name="historicalRecord">告警记录存档接口</param>
    public static void AlertBus(this IContainerRegistry containerRegistry, Type alertsFactory, Type realTimeAlert,
        Type? alertClassifyDb = null, Type? historicalRecord = null)
    {
        containerRegistry.Register(typeof(IAlertBus<,>), typeof(AlertBus<,>));
        containerRegistry.Register(typeof(IAlertsFactory<,>), alertsFactory);
        containerRegistry.Register(typeof(IRealTimeAlertList<,>), realTimeAlert);
        if (alertClassifyDb is not null)
            containerRegistry.RegisterSingleton(typeof(IAlertClassifyDb<>), alertClassifyDb);
        if (historicalRecord is not null)
            containerRegistry.RegisterSingleton(typeof(IHistoricalRecord<,>), historicalRecord);
    }


    public static void AlertBus(this IContainerRegistry containerRegistry, Type realTimeAlert)
    {
        containerRegistry.AlertBus(typeof(DefaultAlertsFactory<,>), realTimeAlert);
    }
}